package com.servlet;

import com.pojo.Student;
import com.service.Service;
import com.utils.Myutils;
import com.utils.Toolutils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@MultipartConfig
@WebServlet("/studentServlet")
public class Servlet extends BaseServlet{
    private Service service=new Service();

    //查询

    /**
     * 添加学生
     * @param req
     * @param resp
     */
    public void insertStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        System.out.println("sex = " + sex);
        String bir = req.getParameter("sbir");
        String[] hobbies = req.getParameterValues("hobby");

        //处理参数 hobbies转成字符串 sbir 转成Date
        String hobby = Myutils.arrayToString(hobbies);
        Date sbir = Myutils.stringToDate(bir);

        //封装实体类
        Student s = new Student(sname, sex, sbir, hobby);

        //接收图片
        Part part = req.getPart("photo");
        //获取图片名称
        String photoname = part.getSubmittedFileName();
        System.out.println("photoname = " + photoname);
        /*通过图片名称 photoname
        判断前台是否传图片
        “” 说明没上传
        否则上传 接着判断是否是.jpg 或者.png
         */
        if (!"".equals(photoname) && (photoname.endsWith(".jpg") ||photoname.endsWith(".png"))){
            photoname = UUID.randomUUID() + photoname;
            System.out.println(photoname);
            //创建本地文件夹
            File path = new File("E://photo");
            if (!path.exists()){
                path.mkdirs();
                System.out.println("进来了");
            }
            //将图片写到文件夹中  E://photo/1.jpg
            part.write(path+"/"+photoname);
        }
        //将图片的名称 添加到实体类中
        s.setPhoto(photoname);

        //调用service
        int i = service.insertStudent(s);
        System.out.println(i);
        if (i>0){
            System.out.println("添加成功 跳转到主页显示所有信息");
            req.getRequestDispatcher("querystudent.jsp").forward(req,resp);
        }else{
            System.out.println("添加失败");
        }
    }

    /**
     * 通过sid查询学生信息   进行回显界面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    //先通过 查询sid 进行要修改的界面
    public void queryStudentBySid(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("sid");
        int sid = Integer.parseInt(id);

        Student s = service.queryStudentBySid(sid);

        req.setAttribute("student",s);
        req.getRequestDispatcher("update.jsp").forward(req,resp);
    }

    /**
     * 确定修改
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    //点击提交按钮
    public void updateStudentBysid(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException {
        //sid sname sex hobby sbir photo
        //接收参数

        String id = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String[] hobbies = req.getParameterValues("hobby");
        String bir = req.getParameter("sbir");
        //接收老的图片名称
        String oldphoto = req.getParameter("oldphoto");

        //转参数
        int sid = Integer.parseInt(id);
        String hobby = Myutils.arrayToString(hobbies);
        Date sbir = Myutils.stringToDate(bir);

        //封装实体类
        Student s = new Student(sname, sex, sbir, hobby);
        s.setSid(sid);

        //处理图片
        Part part = req.getPart("photo");
        //获取图片名称  没有传递图片获取的是""
        String photoname = part.getSubmittedFileName();//11.jpg
        if (!"".equals(photoname) && photoname!=null ){
            photoname = UUID.randomUUID() + photoname;
            part.write("E://photo/"+photoname);
            s.setPhoto(photoname);
        }else {
            s.setPhoto(oldphoto);
        }
        //调用service方法
        int i=service.updateStudentBysid(s);

        //最后修改成功  删除老图片
        if (i>0){
            //修改成功 删除老图片
            //进行判断 传递了新的图片才将老图片进行删除  没有传递  不删除老的图片
            if (photoname!=null && !"".equals(photoname)){
            new File("E://photo/"+oldphoto).delete();
            }
            //直接调用分页查询的方法
            resp.sendRedirect("studentServlet?mark=dimQueryStudent");
        }else{
            System.out.println("删除失败");
        }



    }

    /**
     * 批量删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteStudent(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("nihao");
        String sid = req.getParameter("sid1");
        System.out.println("sid1 = " + sid);
        int i = service.deleteStudent(sid);
        if (i>0){
            req.getRequestDispatcher("studentServlet?mark=dimQueryStudent").forward(req,resp);
            System.out.println("成功删除"+i+"条数据");
        }else{
            System.out.println("删除失败");
        }
    }


    /**
     * 通过sid 删除学生
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteStudentBySid(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        int i = service.deleteStudentBySid(sid);
        if (i>0){
            System.out.println("删除成功");
            //删除学生成功后刷新页面
            req.getRequestDispatcher("studentServlet?mark=dimQueryStudent").forward(req,resp);
        }else {
            System.out.println("删除失败");
        }
    }

    /**
     *  模糊搜索 //先获取到满足条件sex sname 的总条数sumcount
     *
     * @param req
     * @param resp
     */
    public void dimQueryStudent(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        //分页需要准备3个参数
        //当前页
        String currentpage = req.getParameter("currentpage");
        int cp;
        if ("".equals(currentpage) || currentpage==null){
            cp=1;
        }else{
            cp=Integer.parseInt(currentpage);
        }
        //每页显示的条数
        int pagecount=3;
        //总条数 不是表中的所有数据 而是满足sex sname 条件的所有数据
        int sumcount=service.dimQueryStudent(sname,sex);
        //封装分页工具类
        Toolutils tl = new Toolutils(pagecount, sumcount, cp);
        //调用service方法
        List list= service.souSuoStudent(sname,sex,tl);
        //放到作用域中
        req.setAttribute("studentLists",list);
        //把ts 放到作用域中
        req.setAttribute("tl",tl);
        //把两个条件在带回去
        req.setAttribute("sname",sname);
        req.setAttribute("sex",sex);

        req.getRequestDispatcher("querystudent.jsp").forward(req,resp);
    }
}
